﻿//using System;
//using System.Collections;
//using System.Collections.Generic;
//using System.Data;
//using System.Linq;
//using System.Reflection;
//using System.Text;

//TODO:Convert vb to c# partially complete

//namespace zielonka.co.uk.extensions.system.data
//{
//    // Description: Wraps the usage of some DataTable.DefaultView properties to return sorted rows filtered on a custom filterexpression. For documentation on what to put in the whereExpression and the orderByExpression, refer to the MSDN documentation of DataTable.DefaultView.RowFilter and DataTable.DefaultView.Sort.
//    /*
//    Sub Main()
//        Dim dt As New DataTable
//        dt.Columns.AddRange(New DataColumn() {New DataColumn("ColName", GetType(System.String)), _
//                                              New DataColumn("ColDataType", GetType(System.String)), _
//                                              New DataColumn("ColType", GetType(System.String)), _
//                                              New DataColumn("IntCol", GetType(System.Int32)), _
//                                              New DataColumn("DateCol", GetType(System.DateTime)) _
//                                              })
//        dt.Rows.Add("Leo", "DataType", "IntType", 50, Today)
//        dt.Rows.Add("Rose", "StrDataType", "StrType", 90, Now)
//        dt.Rows.Add("Alice", "StrDataType", DBNull.Value, 150, DateAdd(DateInterval.Day, 55, Today))
//        dt.Rows.Add("Emy", "StrDataType", "StrType", DBNull.Value, DateAdd(DateInterval.Day, 55, Today))
//        dt.Rows.Add("Amy", "StrDataType", "StrType", 150, DBNull.Value)

//        Dim rtnList = dt.CopyToEntityList(Of test)()

//        For Each item In rtnList
//            Console.WriteLine(String.Format("ColName = {0}, ColDataType = {1}, ColType = {2}, IntCol = {3}, DateCol = {4}", _
//                                            item.ColName, item.ColDataType, item.ColType, item.IntCol, item.DateCol))
//        Next

//        Console.ReadLine()
//    End Sub

//''' Test is test class, have 5 properties, 2 properties allow nullable, like below: 
//        Private _ColName As String
//        Private _ColDataType As String
//        Private _ColType As String
//        Private _IntCol As Integer?
//        Private _DateCol As DateTime?
//    */

//    public static partial class DataTableExtensions
//    {
//        public static IEnumerable<T> CopyToEntityList<T>(DataTable dt) where T : new()
//        {
//            dynamic myPpts = new T().GetType().GetProperties();
//            dynamic colList = from col in dt.Columns.Cast<DataColumn>() col.ColumnNamecol.DataType;
//            dynamic pptList = (from ppt in myPpt swhere colList.Select(p => p.ColumnName).Contains(ppt.Name) & colList.Select(p => p.DataType).Contains(Nullable.GetUnderlyingType(ppt.PropertyType) == null ? ppt.PropertyType : Nullable.GetUnderlyingType(ppt.PropertyType))ppt);

//            List<T> NewList = new List<T>();
//            foreach (DataRow drSourceItem in dt.Rows) {
//                T NewT = new T();
//                foreach (PropertyInfo entityItem in pptList) {
//                    entityItem.SetValue(NewT, Information.IsDBNull(drSourceItem[entityItem.Name]) ? null : drSourceItem[entityItem.Name], null);
//                }
//                NewList.Add(NewT);
//            }

//            return NewList;
//        }
//    }
//}
